//输入某二叉树的前序遍历和中序遍历的结果，请构建该二叉树并返回其根节点。 
//
// 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 
//
// 
//
// 示例 1: 
//
// 
//Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
//Output: [3,9,20,null,null,15,7]
// 
//
// 示例 2: 
//
// 
//Input: preorder = [-1], inorder = [-1]
//Output: [-1]
// 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 5000 
//
// 
//
// 注意：本题与主站 105 题重复：https://leetcode-cn.com/problems/construct-binary-tree-from-
//preorder-and-inorder-traversal/ 
// Related Topics 树 数组 哈希表 分治 二叉树 👍 771 👎 0


package leetcode.editor.cn;

import com.leetcode.utils.TreeNode;

import java.util.HashMap;
import java.util.Map;

//Java：重建二叉树
public class ZhongJianErChaShuLcof {
    public static void main(String[] args) {
        Solution solution = new ZhongJianErChaShuLcof().new Solution();
        // TO TEST
//        TreeNode treeNode = solution.buildTree(new int[]{3, 9, 20, 15, 7}, new int[]{9, 3, 15, 20, 7});
//        System.out.println(treeNode.val);
        TreeNode treeNode = solution.buildTree(new int[]{3, 9, 10, 11, 20, 15, 7}, new int[]{3, 9, 10, 11, 20, 15, 7});
        System.out.println(treeNode.val);
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    class Solution {
//        /**
//         * @param preorder 前序遍历结果：根左右
//         * @param inorder  中序遍历结果：左根右
//         * @return 请构建该二叉树并返回其根节点
//         */
//        public TreeNode buildTree(int[] preorder, int[] inorder) {
//            if (preorder.length == 0 || inorder.length == 0)
//                return null;
//            if (preorder.length == 1) {
//                TreeNode temp = new TreeNode(preorder[0]);
//                return temp;
//            }
//            int clen = preorder.length;
//            TreeNode root = new TreeNode(preorder[0]);
//            int i = 0;
//            for (; i < clen; i++) {
//                if (inorder[i] == preorder[0]) {
//                    break;
//                }
//            }
//
//            // 以左节点为根的，前序和中序
//            int subPreLen = i;
//            int subInLen = clen - subPreLen - 1;
//            int[] leftPreOrder = new int[subPreLen];
//            int[] leftInOrder = new int[subPreLen];
//            if (subPreLen > 0) {
//                System.arraycopy(preorder, 1, leftPreOrder, 0, subPreLen);
//                System.arraycopy(inorder, 0, leftInOrder, 0, subPreLen);
//            }
//            int[] rightPreOrder = new int[subInLen];
//            int[] rightInOrder = new int[subInLen];
//            if (subInLen > 0) {
//                System.arraycopy(preorder, subPreLen + 1, rightPreOrder, 0, subInLen);
//                System.arraycopy(inorder, subPreLen + 1, rightInOrder, 0, subInLen);
//            }
//            root.left = buildTree(leftPreOrder, leftInOrder);
//            // 以右节点为根的，前序和中序
//            root.right = buildTree(rightPreOrder, rightInOrder);
//            return root;
//        }

        private Map<Integer, Integer> indexMap;

        /**
         * @param preorder 前序遍历结果：[根节点,[左子树节点]，[右子树节点]]
         * @param inorder  中序遍历结果：[[左子树节点]，根节点，[右子树节点]]
         * @return
         */
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            int n = preorder.length;
            // 根节点存储目录
            indexMap = new HashMap<>();
            for (int i = 0; i < n; i++) {
                indexMap.put(inorder[i], i);
            }

            // 构建根节点
            return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1);
        }

        public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
            if (preorder_left > preorder_right)
                return null;
            int rootVal = preorder[preorder_left];
            TreeNode root = new TreeNode(rootVal);
            int rootIndex = indexMap.get(rootVal);
            int leftNodeNum = rootIndex - inorder_left;

            // 左子树节点范围
            root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + leftNodeNum, inorder_left, rootIndex - 1);
            // 右子树节点范围
            root.right = myBuildTree(preorder, inorder, preorder_left + leftNodeNum + 1, preorder_right, rootIndex + 1, inorder_right);
            return root;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
